﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace htmltortf
{
    public partial class Master : Form
    {
        FormState fs = new FormState();
        int top;
        int formcolor;
        public Master()
        {
            InitializeComponent();
        }

        private void Master_Load(object sender, EventArgs e)
        {
            //Criptografia cripto = new Criptografia();
            //if (cripto.EncryptConnStr()) 
            //{
            //    MessageBox.Show("Configuração efetuada, abra novamente o programa");
            //    Application.Exit();
            //} //criptografa connection string
            //cripto.DecryptConnStr(); //descriptografa connection string
            helpProvider1.HelpNamespace = Directory.GetCurrentDirectory() + "\\biblia.chm";
            helpProvider1.SetHelpNavigator(this,HelpNavigator.Topic);
            //if (!SIG.AppCode.UACSecurity.IsAdmin() )//&& SIG.AppCode.UACSecurity.IsVistaOrHigher())
            //{ SIG.AppCode.UACSecurity.RestartElevated(); }//Application.Exit(); }
            //if (!SIG.AppCode.UACSecurity.IsAdmin() )//&& SIG.AppCode.UACSecurity.IsVistaOrHigher())
            //{ Application.Exit(); }
            Criptografia cripto = new Criptografia();
            try
            {
                if (cripto.EncryptConnStr())
                {
                    MessageBox.Show("Configuração efetuada com sucesso, abra novamente o programa");
                    Application.Exit();
                }
            }
            catch { MessageBox.Show("Erro no arquivo de configuração, verifique a seção configuração do manual."); Application.Exit(); }
            //if (cripto.Permissao("igreja.sdf"))
            //{
              //  MessageBox.Show("Configuração efetuada com sucesso");
            //}
            Sobre s = new Sobre();
            AppDomain.CurrentDomain.SetData("DataDirectory", System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) +"\\"+ s.CompanyName+"\\"+s.ProductName+"\\"); 
            //MessageBox.Show(AppDomain.CurrentDomain.GetData("DataDirectory").ToString());
            rtbConteudo.Width = this.Width - 35;
            rtbConteudo.Height = this.Height - 110;
            top = rtbConteudo.Top;
            formcolor = this.BackColor.ToArgb();
            cmbTestamento.Text = "Velho";
            // TODO: This line of code loads data into the 'livro.Livros' table. You can move, or remove it, as needed.
            this.livrosTableAdapter.FillByTestamento(this.livro.Livros,cmbTestamento.Text);
            ConfiguracaoTableAdapters.ConfiguracaoTableAdapter cta =new ConfiguracaoTableAdapters.ConfiguracaoTableAdapter();
            Configuracao.ConfiguracaoDataTable cdt = cta.GetDataById(1);
            Configuracao.ConfiguracaoRow cr = (Configuracao.ConfiguracaoRow)cdt.Rows[0];
            FontStyle style = new FontStyle();//rtbConteudo.SelectionFont.Style;
            if (cr.bold)
            {
                style |= FontStyle.Bold;
            }
            if (cr.italic)
            { style |= FontStyle.Italic; }
            if (cr.strikeout) { style |= FontStyle.Strikeout; }
            if (cr.underline) { style |= FontStyle.Underline; }
            try
            {
                Font f = new Font(cr.font, (float)cr.fontsize, style);

                rtbConteudo.Font = f;
                fontDialog1.Font = f;
            }
            catch { }
            fontDialog1.Color = System.Drawing.ColorTranslator.FromHtml(cr.fontcolor);
            colorDialog1.Color = System.Drawing.ColorTranslator.FromHtml(cr.backcolor);
            //rtbConteudo.SelectAll();
            //rtbConteudo.SelectionFont.Bold = cr.bold;
            //rtbConteudo.SelectionFont.Italic = cr.italic;
            //rtbConteudo.SelectionFont.Strikeout = cr.strikeout;
            //rtbConteudo.SelectionFont.Underline = cr.underline;
          //  try
            //{
              //  rtbConteudo.BackColor = Color.FromName(cr.backcolor);
            //}
            //catch {
            rtbConteudo.BackColor = System.Drawing.ColorTranslator.FromHtml(cr.backcolor); 
            //}
            //try
            //{
               // rtbConteudo.ForeColor = Color.FromName(cr.fontcolor);
            //}
            //catch {
            rtbConteudo.ForeColor = System.Drawing.ColorTranslator.FromHtml(cr.fontcolor); 
        //}
                //f.Bold = cr.bold;
                //rtbConteudo.Font.FontFamily = FontFamily.Families;
                //rtbConteudo.Font.Italic = true;
                //rb

            //LivroTableAdapters.LivrosTableAdapter lta = new LivroTableAdapters.LivrosTableAdapter();
            //Livro.LivrosDataTable ldt = lta.GetDataByTestamento(cmbTestamento.SelectedText);
            //for (int i = 0; i < ldt.Rows.Count; i++)
            //{
              //  Livro.LivrosRow lr = (Livro.LivrosRow)ldt.Rows[i];
                //ToolStripMenuItem item = new ToolStripMenuItem();
                //item.Text = lr.livro;
                //item.Tag = lr.livro_id;
                

                //item.Click += new EventHandler(MetodoQueRecebeClick_Click);
                //menu.Items.Add(item);
            //}      

        }
        public void MetodoQueRecebeClick_Click(object sender, EventArgs e)
        {

                //string nomeForm = ((ToolStripMenuItem)sender).Name;
                //Form f = (Form)Activator.CreateInstance(null, " SUBTITUIR POR NAMESPACE" + nomeForm).Unwrap();
                //f.MdiParent = this;
                //f.Show();

        }

        private void cmbTestamento_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.livrosTableAdapter.FillByTestamento(this.livro.Livros, cmbTestamento.Text);
            BindCapitulo();
        }

        private void cmbLivro_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCapitulo();
        }

        private void BindCapitulo()
        {
            try
            {
                ConteudoTableAdapters.ConteudoTableAdapter cta = new htmltortf.ConteudoTableAdapters.ConteudoTableAdapter();
                Conteudo.ConteudoDataTable cdt = cta.GetDataByCapituloMax(Convert.ToInt32(cmbLivro.SelectedValue));
                nudCapitulo.Maximum = Convert.ToDecimal(cdt.Rows[0]["capitulo"].ToString());
                BindVersiculo();
            }
            catch { }
        }

        private void BindVersiculo()
        {
            ConteudoTableAdapters.ConteudoTableAdapter cta = new ConteudoTableAdapters.ConteudoTableAdapter();
            Conteudo.ConteudoDataTable cdt = cta.GetDataByVersiculoMax(Convert.ToInt32(cmbLivro.SelectedValue), Convert.ToInt32(nudCapitulo.Value));
            nudVerfinal.Maximum = Convert.ToDecimal(cdt.Rows[0]["versiculo"].ToString());
            nudVerinicial.Maximum = Convert.ToDecimal(cdt.Rows[0]["versiculo"].ToString());
            nudVerinicial.Value = 1;
            nudVerfinal.Value = nudVerfinal.Maximum;
        }

        private void btnExibir_Click(object sender, EventArgs e)
        {
            ConteudoTableAdapters.ConteudoTableAdapter cta = new ConteudoTableAdapters.ConteudoTableAdapter();
            Conteudo.ConteudoDataTable cdt = cta.GetDataByLivroCapitulo(Convert.ToInt32(cmbLivro.SelectedValue), Convert.ToInt32(nudCapitulo.Value),Convert.ToInt32(nudVerinicial.Value),Convert.ToInt32(nudVerfinal.Value));
            rtbConteudo.Text = "";
            rtbConteudo.Text = cmbLivro.Text + " - Capitulo " + nudCapitulo.Value;
            if (nudVerinicial.Value > 0)
            {
                rtbConteudo.Text += " : " + nudVerinicial.Value;
                if (nudVerfinal.Value > 0)
                {
                    rtbConteudo.Text+=" - " + nudVerfinal.Value;
                }
            }
            rtbConteudo.Text += "\n";
            for (int i = 0; i < cdt.Rows.Count; i++)
            {
                Conteudo.ConteudoRow cr = (Conteudo.ConteudoRow)cdt.Rows[i];
                rtbConteudo.Text += cr.versiculo+" - "+ cr.palavra + "\n" ;
            }
            
        }

        private void fonteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() != DialogResult.Cancel)
            {
                rtbConteudo.Font = fontDialog1.Font;
                rtbConteudo.ForeColor = fontDialog1.Color;
                ConfiguracaoTableAdapters.ConfiguracaoTableAdapter cta = new ConfiguracaoTableAdapters.ConfiguracaoTableAdapter();
                Configuracao.ConfiguracaoDataTable cdt = cta.GetDataById(1);
                Configuracao.ConfiguracaoRow cr = (Configuracao.ConfiguracaoRow)cdt.Rows[0];
                cr.bold = rtbConteudo.Font.Bold;
                cr.font = rtbConteudo.Font.FontFamily.Name;
                cr.fontcolor = rtbConteudo.ForeColor.ToArgb().ToString();
                //cr.fontcolor = rtbConteudo.ForeColor.Name;
                cr.fontsize = rtbConteudo.Font.Size;
                cr.italic = rtbConteudo.Font.Italic;
                cr.strikeout = rtbConteudo.Font.Strikeout;
                cr.underline = rtbConteudo.Font.Underline;
                cta.Update(cr);
                //cta.Update(rtbConteudo.Font.FontFamily.Name,rtbConteudo.Font.Size,rtbConteudo.Font.Bold,rtbConteudo.Font.Italic,"","",rtbConteudo.Font.Underline,fontDialog1.Font.Strikeout,1);
                
            }
        }

        private void corToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                rtbConteudo.ForeColor = colorDialog1.Color;
                ConfiguracaoTableAdapters.ConfiguracaoTableAdapter cta = new ConfiguracaoTableAdapters.ConfiguracaoTableAdapter();
                Configuracao.ConfiguracaoDataTable cdt = cta.GetDataById(1);
                Configuracao.ConfiguracaoRow cr = (Configuracao.ConfiguracaoRow)cdt.Rows[0];
                cr.fontcolor = rtbConteudo.ForeColor.ToArgb().ToString();
                cta.Update(cr);
            }
        }

        private void corFundoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                rtbConteudo.BackColor = colorDialog1.Color;
                ConfiguracaoTableAdapters.ConfiguracaoTableAdapter cta = new ConfiguracaoTableAdapters.ConfiguracaoTableAdapter();
                Configuracao.ConfiguracaoDataTable cdt = cta.GetDataById(1);
                Configuracao.ConfiguracaoRow cr = (Configuracao.ConfiguracaoRow)cdt.Rows[0];
                cr.backcolor = rtbConteudo.BackColor.ToArgb().ToString();
                cta.Update(cr);
            }
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Sobre frmSobre = new Sobre();
            frmSobre.ShowDialog();
        }

        private void Master_KeyUp(object sender, KeyEventArgs e)
        {
           /*  if (e.KeyCode == Keys.F11)
            {
                FormState fs = new FormState();
                if (FullScreenMode)
                {
                    FullScreenMode = false;
                    fs.Restore(this);
                }
                else
                { fs.Maximize(this); FullScreenMode = true; }
               if (FullScreenMode == false)
                {
                   // Form2 f = new Form2();
                    //this.Owner = f;
                    this.FormBorderStyle = FormBorderStyle.None;
                    this.Left = (Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2);
                    this.Top = (Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);
                    //f.Show();
                    
                    FullScreenMode = true;
                    //this.KeyUp += new KeyEventHandler(Master_KeyUp);
                    //f.KeyUp += new KeyEventHandler(Form1_KeyUp);
                }
                else
                {
                    //Form f = this.Owner;
                    //this.Owner = null;
                    //f.Close();

                    this.FormBorderStyle = FormBorderStyle.Sizable;
                    FullScreenMode = false;
                }
            }*/
        }

        private void Master_KeyDown(object sender, KeyEventArgs e)
        {
            /*if (e.KeyCode == Keys.F11)
            {
                FormState fs = new FormState();
                if (FullScreenMode)
                {
                    FullScreenMode = false;
                    fs.Restore(this);
                }
                else
                { fs.Maximize(this); FullScreenMode = true; }
            }*/
        }

        private void rtbConteudo_KeyUp(object sender, KeyEventArgs e)
        {
          
            if (e.KeyCode == Keys.Escape && rtbConteudo.BorderStyle==BorderStyle.None)
            {                          
                    //FullScreenMode = false;
                    fs.Restore(this);
                    //foreach (Control item in this.Controls)
                    //{
                    //    item.Visible = true;
                    //}
                    cmbLivro.Visible = true;
                    cmbTestamento.Visible = true;
                    label1.Visible = true;
                    label2.Visible = true;
                    label3.Visible = true;
                    label4.Visible = true;
                    label5.Visible = true;
                    nudCapitulo.Visible = true;
                    nudVerinicial.Visible = true;
                    nudVerfinal.Visible = true;
                    menu.Visible = true;
                    rtbConteudo.Visible = true;
                    btnExibir.Visible = true;
                    rtbConteudo.Focus();
                    rtbConteudo.Top = top;
                    rtbConteudo.Width = this.Width - 35;
                    rtbConteudo.Height = this.Height - 110;
                    this.BackColor = System.Drawing.ColorTranslator.FromHtml(formcolor.ToString());
                    rtbConteudo.BorderStyle = BorderStyle.Fixed3D;
            }     
            //else if(e.KeyCode==Keys.F11)
            //{
            //    Fullscreen();
            //}
        }

        private void Fullscreen()
        {
            fs.Maximize(this);
            //FullScreenMode = true;
            //foreach (Control item in this.Controls)
            //{
            //    item.Visible = false;
            //}
            cmbLivro.Visible = false;
            cmbTestamento.Visible = false;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            nudCapitulo.Visible = false;
            nudVerinicial.Visible = false;
            nudVerfinal.Visible = false;
            menu.Visible = false;
            rtbConteudo.Visible = true;
            btnExibir.Visible = false;
            rtbConteudo.Focus();
            rtbConteudo.Top = 5;
            rtbConteudo.Width = this.Width - 15;
            rtbConteudo.Height = this.Height - 15;
            rtbConteudo.BorderStyle = BorderStyle.None;
            this.BackColor = rtbConteudo.BackColor;
        }

        private void telaInteiraToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Fullscreen();
        }

        private void nudCapitulo_ValueChanged(object sender, EventArgs e)
        {
            BindVersiculo();
        }

        private void ajudaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{F1}");
        }

        private void slideShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Slide frmSlide = new Slide();
            frmSlide.Carregar(Convert.ToInt32(cmbLivro.SelectedValue),nudCapitulo.Value,nudVerfinal.Value,nudVerinicial.Value);
            frmSlide.Show();
        }
    }
}
